package com.example.bilibili_fake.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import android.widget.ProgressBar
import android.widget.Toast
import androidx.core.graphics.toColorInt
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.example.bilibili_fake.databinding.FragmentHomeBinding
import com.example.bilibili_fake.ui.SearchActivity
import com.example.bilibili_fake.utils.LogUtil
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.youth.banner.Banner
import com.youth.banner.config.IndicatorConfig
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.indicator.Indicator
import kotlin.collections.isNotEmpty


class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    private lateinit var homeViewModel: HomeViewModel
    private lateinit var progressBar: ProgressBar
    private lateinit var errorText: TextView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        homeViewModel = ViewModelProvider(this)[HomeViewModel::class.java]

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        progressBar = binding.progressBar
        errorText = binding.errorText

        setupViewPager()
        observeViewModel()

        binding.searchButton.setOnClickListener {
            val intent = android.content.Intent(this.requireContext(), SearchActivity::class.java)
            startActivity(intent)
        }
        return root
    }

    private fun setupViewPager() {
        val fragments = listOf(
            com.example.bilibili_fake.ui.live.LiveFragment.newInstance(),
            RecommendFragment.newInstance(),
            com.example.bilibili_fake.ui.hot.HotFragment.newInstance(),
            com.example.bilibili_fake.ui.anime.AnimeFragment.newInstance(),
            com.example.bilibili_fake.ui.film.FilmFragment.newInstance()
        )

        val adapter = ViewPagerAdapter(requireActivity(), fragments)
        binding.viewPager.adapter = adapter
        // 设置初始选中项为推荐
        binding.viewPager.currentItem = 1

        TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position ->
            when (position) {
                0 -> tab.text = "直播"
                1 -> tab.text = "推荐"
                2 -> tab.text = "热门"
                3 -> tab.text = "动画"
                4 -> tab.text = "影视"
            }
        }.attach()
    }

    private fun observeViewModel() {
        homeViewModel.loading.observe(viewLifecycleOwner) { isLoading ->
            progressBar.visibility = if (isLoading) View.VISIBLE else View.GONE
        }

        homeViewModel.error.observe(viewLifecycleOwner) { error ->
            if (error != null) {
                errorText.text = error
                errorText.visibility = View.VISIBLE
                Toast.makeText(context, error, Toast.LENGTH_LONG).show()
                LogUtil.e("HomeFragment", error)
            } else {
                errorText.visibility = View.GONE
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}